package com.kingsalert.kingsalert.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.view.NumberTextView_L;

import java.util.ArrayList;


public class HomeCarouselAdapter extends RecyclerView.Adapter<HomeCarouselAdapter.HomeCarouselHolder> {

    private final Context mContext;
    private final ArrayList<HomeCarouseInfo> infos;

    public HomeCarouselAdapter(Context context,ArrayList<HomeCarouseInfo> infos){
        mContext = context;
        this.infos = infos;
    }

    @NonNull
    @Override
    public HomeCarouselHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HomeCarouselHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_carousel,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull HomeCarouselHolder holder, int position) {

        int size = infos.size();
        int index=position%size;

        HomeCarouseInfo info = infos.get(index);

        holder.setAmount(info.getAmount());
        holder.initPointGroup(index,size);
        holder.setLock(info.isLock());

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public static class HomeCarouselHolder extends RecyclerView.ViewHolder{

        private final View mView;

        private final NumberTextView_L numberTextViewL;
        private final LinearLayout pointgroupLayout;
        private final View lockView;

        @SuppressLint("InflateParams")
        public HomeCarouselHolder(View view){
            super(view);
            mView = view;

            numberTextViewL = mView.findViewById(R.id.ihc_view_number_text);
            pointgroupLayout = mView.findViewById(R.id.ihc_layout_point_group);
            lockView = mView.findViewById(R.id.ihc_view_lock);
        }

        public void setAmount(CharSequence amount){
            numberTextViewL.setText(amount);
        }

        public void initPointGroup(int index,int max){
            pointgroupLayout.removeAllViews();
            for (int i=0;i<max;i++){
                View pointView = LayoutInflater.from(mView.getContext()).inflate(R.layout.view_blank,null);
                pointView.setBackgroundResource(index==i ? R.drawable.bg_home_carousel_point_true:R.drawable.bg_home_carousel_point_false);
                pointgroupLayout.addView(pointView);
            }
        }

        public void setLock(boolean isLock){
            lockView.setVisibility(isLock ? View.VISIBLE:View.GONE);
        }
    }

    public static class HomeCarouseInfo{

        private final String amount;
        private final boolean isLock;

        public HomeCarouseInfo(String amount, boolean isLock) {
            this.amount = amount;
            this.isLock = isLock;
        }

        public String getAmount() {
            return amount;
        }

        public boolean isLock() {
            return isLock;
        }
    }
}
